package com.test.springboot.springboot3testdemo3.service;

import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import com.test.springboot.springboot3testdemo3.entity.User;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/9/14-09:20
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Service
public class JetCacheUserService {
  private static final Logger LOGGER = LoggerFactory.getLogger(JetCacheUserService.class);
  private static final String USER_CACHE_PREFIX = "user:";

  @Cached(name = USER_CACHE_PREFIX, key = "#user.id", expire = 600, timeUnit = TimeUnit.SECONDS)
  public User save(User user) {
    LOGGER.info("保存用户信息");
    return user;
  }

  @Cached(name = USER_CACHE_PREFIX, key = "#userId")
  public User get(long userId) {
    LOGGER.info("查询用户信息");
    return new User();
  }

  @CacheUpdate(name = USER_CACHE_PREFIX, key = "#user.id", value = "#user")
  public User updateUser(User user) {
    LOGGER.info("更新用户信息");
    return user;
  }

  @CacheInvalidate(name = USER_CACHE_PREFIX, key = "#userId")
  public void deleteUser(long userId) {
    LOGGER.info("删除用户【%s】%n", userId);
  }
}
